package com.common.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;

/**
 * @类名 JsonSchemaValidator
 * @描述 校验json数据格式
 * @版本 a
 * @创建人Lying~
 * @创建时间 2023/3/17 14:39
 * @修改人 Lying~
 * @修改时间 2023/3/17 14:39
 */
@Slf4j
public class JsonSchemaValidatorUtil {

    private static Boolean toJsonSchemaValidator(String walMartToBulkItemSetupJson,File file) throws ProcessingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode schemaNode = mapper.readTree(file);
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        JsonSchema schema = factory.getJsonSchema(schemaNode);
        JsonNode dataNode = mapper.readTree(walMartToBulkItemSetupJson);
        ProcessingReport report = schema.validate(dataNode);
        if (!report.isSuccess()) {
            log.error("当前校验失败，原因为：【{}】", report);
            // 验证失败，处理错误信息
            return false;
        }
        return true;
    }

    public static Boolean validatorWalJsonSchema(String walMartToBulkItemSetupJson) throws ProcessingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        ClassPathResource schemaResource = new ClassPathResource("MP_ITEM_SPEC_4.6.json");
        File file = new File(schemaResource.getURL().getPath());
        return toJsonSchemaValidator(walMartToBulkItemSetupJson,file);
    }

    public static void main(String[] args) throws IOException, ProcessingException {
        System.out.println(validatorWalJsonSchema("{\n" +
                "  \"MPItemFeedHeader\": {\n" +
                "    \"sellingChannel\": \"marketplace\",\n" +
                "    \"processMode\": \"REPLACE\",\n" +
                "    \"subset\": \"EXTERNAL\",\n" +
                "    \"locale\": \"en\",\n" +
                "    \"version\": \"4.1\",\n" +
                "    \"subCategory\": \"office_other\"\n" +
                "  },\n" +
                "  \"MPItem\": [\n" +
                "    {\n" +
                "      \"Orderable\": {\n" +
                "        \"sku\": \"00078742044555\",\n" +
                "        \"productIdentifiers\": {\n" +
                "          \"productIdType\": \"GTIN\",\n" +
                "          \"productId\": \"00649081365761\"\n" +
                "        },\n" +
                "        \"productName\": \"SmileMart 5-Tier Adjustable Metal Garage Storage Rack\",\n" +
                "        \"brand\": \"SmileMart\",\n" +
                "        \"price\": 140,\n" +
                "        \"ShippingWeight\": 31,\n" +
                "        \"electronicsIndicator\": \"No\",\n" +
                "        \"batteryTechnologyType\": \"Does Not Contain a Battery\",\n" +
                "        \"chemicalAerosolPesticide\": \"No\",\n" +
                "        \"shipsInOriginalPackaging\": \"No\",\n" +
                "        \"startDate\": \"2019-01-01T08:00:00Z\",\n" +
                "        \"endDate\": \"2060-01-01T08:00:00Z\",\n" +
                "        \"MustShipAlone\": \"No\"\n" +
                "      },\n" +
                "      \"Visible\": {\n" +
                "        \"Office\": {\n" +
                "          \"shortDescription\": \"Storage\",\n" +
                "          \"mainImageUrl\": \"https://i5-qa.walmartimages.com/asr/e267b894-bce0-4603-b0b9-b70da3a6c65b.b3d16cea4de59e10dbf8c181d148d8a3.jpeg\",\n" +
                "          \"productSecondaryImageURL\": [\n" +
                "            \"https://i5-qa.walmartimages.com/asr/c053a126-34f2-4a26-bb14-c0b40af56685.ea3111241d98269f7486422e21e8f9fd.jpeg\",\n" +
                "            \"https://i5-qa.walmartimages.com/asr/51abfcc8-1903-4113-bebf-09695dc89532.a2952b0bd51326dc0308186f3e04e70a.jpeg\"\n" +
                "          ],\n" +
                "          \"prop65WarningText\": \"None\",\n" +
                "          \"smallPartsWarnings\": [\n" +
                "            \"0 - No warning applicable\"\n" +
                "          ],\n" +
                "          \"compositeWoodCertificationCode\": \"1 - Does not contain composite wood\",\n" +
                "          \"keyFeatures\": [\n" +
                "            \"<ul><li><h1>17\\\\\\\" LCD Optiplex PC 2 Duo 4GB Mem 160GB HD Windows 10</h1></li><li><h3> Optiplex for Sale</h3></li><li>The Optiplex line of computers includes some of the most versatile PCs on the market. A great choice for business applications, video gaming, and other personal computer.</li></ul>\"\n" +
                "          ],\n" +
                "          \"manufacturer\": \"SmileMart\",\n" +
                "          \"manufacturerPartNumber\": \"optiplexrf\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}"));
    }
}
